<template>
  <div class="cart-box">
    <s-header :title="'购物车'"></s-header>
    <div class="cart-body">
      <van-checkbox-group
        @change="groupChange"
        v-model="result"
        ref="checkboxGroup"
      >
        <van-swipe-cell
          :right-width="50"
          v-for="(item, index) in list"
          :key="index"
        >
          <div class="good-item">
            <van-checkbox :name="item.cartItemId" />
            <div class="good-img">
              <img :src="item.goodCoverImg" />
            </div>
            <div class="good-desc">
              <div class="good-title">
                <span>{{ item.goodName }}</span>
                <span>x{{ item.goodCount }}</span>
              </div>
              <div class="good-btn">
                <div class="price">￥{{ item.sellingprice }}</div>
                <van-stepper
                  integer
                  :min="1"
                  :value="item.goodCount"
                  :name="item.cartItemId"
                  async-change
                  @change="onChange"
                />
              </div>
            </div>
          </div>
          <van-button
            slot="right"
            square
            icon="delete"
            type="danger"
            class="delete-button"
            @click="deleteGood(item.cartItemId)"
          />
        </van-swipe-cell>
      </van-checkbox-group>
    </div>
    <!-- <van-submit-bar
      v-if="list.lenght > 0"
      class="submit-all"
      :price="total * 100"
      button-text="结算"
      @submit="onSubmit"
    > 

      <van-checkbox   v-model="checkAll">全选</van-checkbox>
      <template #tip>
    你的收货地址不支持同城送, <span @click="onClickEditAddress">修改地址</span>
  </template>
    </van-submit-bar> -->


    <van-submit-bar v-if="list.length>0"  :price="100" button-text="结算" @submit="onSubmit">
      <van-checkbox  @click="allCheck"  v-model="checkAll" >全选</van-checkbox>
      <template #tip>
        你的收货地址不支持同城送,
        <span @click="onClickEditAddress">修改地址</span>
      </template>
    </van-submit-bar>



    <div class="empty" v-if="!list.length">
      <van-icon name="smile-o" />
      <div class="title">购物车空空如也</div>
      <van-button color="#c40000" type="primary" @click="goTo" block
        >前往首页</van-button
      >
    </div>
  </div>
</template>

<script>
import sHeader from "@/components/SimpleHeader";
import { Toast } from "vant";
import { getCart, modifyCart, deleteCartItem } from "../service/cart";


export default {
  name: "Cart",
  components: {
    sHeader,
  },
  data() {
    return {
      list: [],
      result: [],
    
      checkAll:true,
    };
  },
  mounted() {
    this.init();
    // console.log(this.list);
  },
  computed: {
    //计算总金额
    total: function () {
      let sum = 0;
      let _list = this.list.filter((item)=>this.result.includes(item,cartItemId) );
      // console.log(this.list)
      _list.forEach((item) => {
        sum += item.goodCount * item.sellingprice;
      });
      return sum;
    }
  },
  // watch:{
  //   result:{
  //     deep:true,
  //     immediate:true,
  //     handler(){
  //        if (this.checkAll) {
  //       this.result = this.list.map((item) => item.cartItemId);
  //       console.log(item.cartItemId);
  //     } else {
  //       this.result = [];
  //     }

  //     }
  //   }

  // },
  methods: {
    onClickEditAddress(){
            Toast.loading;
            this.$router.push({ path:'address' });
    },
    checked(){

    },
    goTo() {
      this.$router.push("/home");
    },
    async init() {
      Toast.loading({ message: "加载中...", forbidClick: true }); //   加载中  禁止点击          //Toast有一定时间限制,过后会消失。
      const { data } = await getCart({ pageNumber: 1 }); //获取购物车商品数据
      this.list = data; //列表数据  赋给this.list
      // console.log(data);
      this.result = data.map((item) => item.cartItemId); //获取每个商品的id
      Toast.clear(); //消除限制
      // console.log(data);
    },
    async deleteGood(id) {
      const { data } = await deleteCartItem(id);
      this.$store.dispatch("updataCart");
      this.init();
    },
    async onChange(value, detail) {
      if (
        this.list.filter((item) => item.cartItemId == detail.name)[0]
          .goodCount == value //filter过滤出满足箭头后的括号内条件时产生新的数组
      )
        return;
      Toast.loading({ message: "修改中...", forbidClick: true });
      const params = {
        cartItemId: detail.name,
        goodCount: value,
      };
      const { data } = await modifyCart(params);
      this.list.forEach((item) => {
        if (item.cartItemId == detail.name) {
          item.goodCount = value;
        }
      });
      Toast.clear();
    },
    groupChange(result) {
      // console.log(result.length);
      // console.log(this.list.length);
      if (result.length == this.list.length) {
        this.checkAll = true;
      } else {
        this.checkAll = false;
      }
      this.result = result;
    },
    async onSubmit() {
      const params = JSON.stringify(this.result);
      this.$router.push({ path: `create-order?cartItemIds=${params}` });
    },
    allCheck(value) {
      if (this.checkAll) {
        this.result = this.list.map((item) => item.cartItemId);
        console.log(item.cartItemId);
      } else {
        this.result = [];
      }
    },
  },
};
</script>


<style lang="less" scoped>
@import "../common/style/mixin";
.van-submit-bar{
  margin-bottom: 50px;
}
.cart-box {
  .cart-body {
    margin: 60px 0 100px 0;
    padding-left: 10px;
    .good-item {
      display: flex;
      .good-img {
        img {
          .wh(100px, 100px);
        }
      }
      .good-desc {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;
        padding: 20px;
        .good-title {
          display: flex;
          justify-content: space-between;
        }
        .good-btn {
          display: flex;
          justify-content: space-between;
          .price {
            font-size: 16px;
            color: red;
            line-height: 28px;
          }
          .van-icon-delete {
            font-size: 20px;
            margin-top: 4px;
          }
        }
      }
    }
    .delete-button {
      width: 50px;
      height: 100%;
    }
  }
  .submit-all {
    margin-bottom: 1px;
    .van-checkbox {
      margin-left: 10px;
    }
    .van-submit-bar_text {
      margin-right: 10px;
    }
    .van-submit-bar_button {
      background: @primary;
    }
  }
  .empty {
    width: 50%;
    margin: 0 auto;
    text-align: center;
    margin-top: 200px;
    .var-icon-smile-o {
      font-size: 50px;
    }
    .title {
      font-size: 16px;
      margin-bottom: 20px;
    }
  }
}
</style>
<style lang="less">
@import "../common/style/mixin";
.van-checkbox_icon--checked .van-icon {
  background-color: @primary;
  border-color: @primary;
}
</style>
